﻿#if No_No_no_Error
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class UIManager : MonoBehaviour
{

	public Image inkImage;

	public static UIManager _uiManager;

	public Animator _animator;

	public Text resultText, levelText, _ballText, _starText;

	public Sprite pauseSpr, musicOn, musicOff, SoundOn, SoundOff;

	public Image musicSpr, SoundSpr;

	public GameObject nextButton, playButton;

	public bool isPlayingBall;



	void Awake ()
	{
		_uiManager = this;
	}

	// Use this for initialization
	void Start ()
	{
		int currentLevel = PlayerPrefs.GetInt ("CurrentLevel");
		isPlayingBall = false;
		levelText.text = "LEVEL " + currentLevel.ToString ();
		LoadSoundAndMusic ();

		if (GameManager._instance._mode == 0)
			playButton.SetActive (true);
		else
			playButton.SetActive (false);
	}
	
	// Update is called once per frame
	void Update ()
	{
		
	}

	public void ActiveBall ()
	{
		if (!isPlayingBall) {
			isPlayingBall = true;
			playButton.GetComponent<Image> ().sprite = pauseSpr;
			GameManager._instance.ActiveBalls ();
		}
	}

	public void SetInkProgress (float _ink)
	{
		inkImage.fillAmount = _ink;
	}

	public void ShowGameOver ()
	{
		StartCoroutine (ShowGameOverIE ());
	}

	public IEnumerator ShowGameOverIE ()
	{
		yield return new WaitForSeconds (1.0f);
		resultText.text = "GAME OVER";
		nextButton.SetActive (false);
		_ballText.text = "X" + GameManager._instance._ball.ToString ();
		_starText.text = "X" + GameManager._instance._star.ToString ();
		_animator.SetBool ("GameOver", true);
		StartCoroutine (ShowAds ());
	}

	public void ShowClear ()
	{
		StartCoroutine (ShowClearIE ());
		AudioManager.Instance.PlayLevelComplete ();
	}

	public IEnumerator ShowClearIE ()
	{
		yield return new WaitForSeconds (1.0f);
		resultText.text = "LEVEL COMPLETE";
		_ballText.text = "X" + GameManager._instance._ball.ToString ();
		_starText.text = "X" + GameManager._instance._star.ToString ();
		int currentLevel = PlayerPrefs.GetInt ("CurrentLevel");
		PlayerPrefs.SetInt ("Level" + GameManager._instance._mode.ToString () + (currentLevel + 1).ToString (), 1);
		_animator.SetBool ("GameOver", true);
		StartCoroutine (ShowAds ());
	}

	public void Back ()
	{
		AudioManager.Instance.PlayClickSound ();
		SceneManager.LoadScene (0);
	}

	public void Replay ()
	{
		AudioManager.Instance.PlayClickSound ();
		SceneManager.LoadScene (1);
	}

	public void Next ()
	{
		AudioManager.Instance.PlayClickSound ();
		int currentLevel = PlayerPrefs.GetInt ("CurrentLevel");
		PlayerPrefs.SetInt ("CurrentLevel", currentLevel + 1);
		SceneManager.LoadScene (1);
	}

	public void ToggleMusic ()
	{
		int toggleMusic = PlayerPrefs.GetInt ("Music");
		if (toggleMusic == 1) {

			//set on
			AudioManager.Instance.ToggleMusic (1);
			PlayerPrefs.SetInt ("Music", 0);
			musicSpr.sprite = musicOn;
		} else {
			//set off
			AudioManager.Instance.ToggleMusic (0);
			PlayerPrefs.SetInt ("Music", 1);
			musicSpr.sprite = musicOff;
		}
	}

	public void ToggleSound ()
	{

		int toggleSound = PlayerPrefs.GetInt ("Sound");
		if (toggleSound == 1) {

			//set on
			AudioManager.Instance.ToggleSound (1);
			PlayerPrefs.SetInt ("Sound", 0);
			SoundSpr.sprite = SoundOn;
		} else {
			//set off
			AudioManager.Instance.ToggleSound (0);
			PlayerPrefs.SetInt ("Sound", 1);
			SoundSpr.sprite = SoundOff;
		}
	}

	void LoadSoundAndMusic ()
	{
		int toggleMusic = PlayerPrefs.GetInt ("Music");
		if (toggleMusic == 1) {

			//set on
			AudioManager.Instance.ToggleMusic (0);
			musicSpr.sprite = musicOff;
		} else {
			//set off
			AudioManager.Instance.ToggleMusic (1);
			musicSpr.sprite = musicOn;
		}


		int toggleSound = PlayerPrefs.GetInt ("Sound");
		if (toggleSound == 1) {

			//set on
			AudioManager.Instance.ToggleSound (0);
			SoundSpr.sprite = SoundOff;
		} else {
			//set off
			AudioManager.Instance.ToggleSound (1);
			SoundSpr.sprite = SoundOn;
		}
	}

	IEnumerator ShowAds ()
	{
		yield return new WaitForSeconds (0.5f);
		AdsControl.Instance.showAds ();
	}
}
#endif